/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package business.service;

import com.nmp.common.CommonUtil;
import com.nmp.common.RestUtil;
import com.nmp.common.UserData;
import com.nmp.model.UserModel;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.log4j.Logger;
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;

/**
 *
 * @author ngotandieu
 */
public class GGClientService {
    
    private static GGClientService instance = null;
    private static final Lock iLock = new ReentrantLock();
    protected final Logger logger = Logger.getLogger(this.getClass());

    private static final String GG_APP_NAME = "gg";
    private static final String GG_SECRECT_KEY = "520f79d51b6cb044dc112e565ebb22b9";
    private static final String GG_APP_ID = "1509402355969408";
    
    public static GGClientService getInstance() {
        iLock.lock();
        try {
            if (instance == null) {
                instance = new GGClientService();
            }
            return instance;
        } finally {
            iLock.unlock();
        }
    }
    
    public String createGGAccount(UserData userData) {
        String id = "";
        try {
            userData.setApp(GG_APP_NAME);
            userData.setCreated(System.currentTimeMillis());
            userData.setDob(0);
            // random password
            String pass = RandomStringUtils.randomAlphanumeric(10);
            userData.setPasswdMD5(CommonUtil.md5(pass));
            
            UserModel userModel = UserModel.getInstance();
            id = userModel.addUser(userData);
        } catch (Exception ex) {
            logger.info("createFBAccount: " + ex.getMessage(), ex);
        }
        return id;
    }
    
    public UserData inspectAccessTokens(String accessToken) {
        UserData userData = null;
        try {
            String restUrl = "https://www.googleapis.com/plus/v1/people/me?access_token=" + accessToken;
            Map<String, String> httpsRest = RestUtil.httpsRest(restUrl);
            if(httpsRest != null) {
                String code = httpsRest.get("respCode");
                if(code.equals("200")) {
                    String data = httpsRest.get("data");
                    JSONParser parser = new JSONParser();
                    Map<String, Object> apiData = (Map) parser.parse(data);
                    userData = new UserData();
                    
                    userData.setApp_user_id("gg." + apiData.get("id"));
                    userData.setLink((String) apiData.get("url"));
                    userData.setName((String) apiData.get("displayName"));
                    
                    String gender = (String) apiData.get("gender");
                    int gd = gender == null ? 0 : (gender.equalsIgnoreCase("male") ? 1 : 2);
                    userData.setGender(gd);
                    
                    JSONArray emails = (JSONArray)apiData.get("emails");
                    Map<String, String> mapEmail = (Map<String, String>) emails.get(0);
                    userData.setEmail(mapEmail.get("value"));
                    
                    Map<String, String> image = (Map) apiData.get("image");
                    userData.setAvatar(image.get("url"));
                    
                    return userData;
                }
            }
        } catch (Exception ex) {
            logger.error("inspectAccessTokens: " + ex.getMessage(), ex);
        }
        return userData;
    }
}
